/**
 * 
 */
package pl.koziolekweb.translator.gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;

import pl.koziolekweb.translator.engine.Language;
import pl.koziolekweb.translator.engine.TranslatorService;
import pl.koziolekweb.translator.engine.TranslatorServiceFactory;
import pl.koziolekweb.translator.gui.controllers.TranslatorController;
import pl.koziolekweb.translator.utilites.Context;

/**
 * @author koziolek
 * 
 */
public class TranslatorPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private Context context;
	private SpringLayout layout;
	private TranslatorServiceFactory translatorServiceFactory;
	private JComboBox languageListFrom;
	private JButton translateButton;
	private JComboBox languageListTo;
	private JTextArea textTo;
	private JTextArea textFrom;
	private List<Language> languages;
	private JLabel translatorTo;
	private JLabel translatorFrom;
	private JComboBox translatorAviableTranslatorsList;
	private TranslatorService translatorService;
	private JLabel translatorAvialableTranslatorsLabel;

	public TranslatorPanel(final Context context) {
		this.context = context;
		createTranslatorPanel();
		context.add("translatorPanel", this);
	}

	private void createTranslatorPanel() {
		setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));

		this.setSize((int) (Integer.parseInt(context.getParam("width")) * 0.3),
				getHeight());
		layout = new SpringLayout();
		setLayout(layout);
		translatorServiceFactory = TranslatorServiceFactory.getFactory();

		Set<String> avaiableTranslatorServices = translatorServiceFactory
				.getServiceList();
		translatorService = translatorServiceFactory
				.getTranslatorService("Google-Translate");
		

		translatorAvialableTranslatorsLabel = new JLabel(context
				.getParam("language.service.list"));
		translatorAviableTranslatorsList = new JComboBox(
				avaiableTranslatorServices.toArray());
		translatorAviableTranslatorsList
				.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						translatorService = translatorServiceFactory
								.getTranslatorService(translatorAviableTranslatorsList
										.getSelectedItem().toString());
					}
				});
		translatorAviableTranslatorsList.setSelectedItem("Google-Translate");
		this.add(translatorAvialableTranslatorsLabel);
		layout.putConstraint(SpringLayout.NORTH,
				translatorAvialableTranslatorsLabel, 13, SpringLayout.NORTH,
				this);
		layout
				.putConstraint(SpringLayout.WEST,
						translatorAvialableTranslatorsLabel, 3,
						SpringLayout.WEST, this);
		this.add(translatorAviableTranslatorsList);
		layout.putConstraint(SpringLayout.NORTH,
				translatorAviableTranslatorsList, 10, SpringLayout.NORTH, this);
		layout.putConstraint(SpringLayout.EAST,
				translatorAviableTranslatorsList, -3, SpringLayout.EAST, this);

		languages = translatorService.getSupportedLanguages();
		translatorFrom = new JLabel(context.getParam("language.from"));
		languageListFrom = new JComboBox(languages.toArray());
		textFrom = new JTextArea();
		textFrom.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));

		this.add(translatorFrom);
		layout.putConstraint(SpringLayout.NORTH, translatorFrom, 13,
				SpringLayout.SOUTH, translatorAviableTranslatorsList);
		layout.putConstraint(SpringLayout.WEST, translatorFrom, 3,
				SpringLayout.WEST, this);
		this.add(languageListFrom);
		layout.putConstraint(SpringLayout.NORTH, languageListFrom, 10,
				SpringLayout.SOUTH, translatorAviableTranslatorsList);
		layout.putConstraint(SpringLayout.WEST, languageListFrom, 3,
				SpringLayout.EAST, translatorFrom);
		this.add(textFrom);
		layout.putConstraint(SpringLayout.NORTH, textFrom, 13,
				SpringLayout.SOUTH, languageListFrom);
		layout.putConstraint(SpringLayout.WEST, textFrom, 3, SpringLayout.WEST,
				this);
		layout.putConstraint(SpringLayout.EAST, textFrom, -3,
				SpringLayout.EAST, this);
		layout.putConstraint(SpringLayout.SOUTH, textFrom, 200,
				SpringLayout.NORTH, textFrom);

		translatorTo = new JLabel(context.getParam("language.to"));
		languageListTo = new JComboBox(languages.toArray());
		textTo = new JTextArea();
		textTo.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));

		this.add(translatorTo);
		layout.putConstraint(SpringLayout.NORTH, translatorTo, 13,
				SpringLayout.SOUTH, textFrom);
		layout.putConstraint(SpringLayout.WEST, translatorTo, 3,
				SpringLayout.WEST, this);
		this.add(languageListTo);
		layout.putConstraint(SpringLayout.NORTH, languageListTo, 10,
				SpringLayout.SOUTH, textFrom);
		layout.putConstraint(SpringLayout.WEST, languageListTo, 3,
				SpringLayout.EAST, translatorTo);
		this.add(textTo);
		layout.putConstraint(SpringLayout.NORTH, textTo, 13,
				SpringLayout.SOUTH, languageListTo);
		layout.putConstraint(SpringLayout.WEST, textTo, 3, SpringLayout.WEST,
				this);
		layout.putConstraint(SpringLayout.EAST, textTo, -3, SpringLayout.EAST,
				this);
		layout.putConstraint(SpringLayout.SOUTH, textTo, 200,
				SpringLayout.NORTH, textTo);

		translateButton = new JButton(context.getParam("translate"));

		context.add("languageListFrom", languageListFrom);
		context.add("languageListTo", languageListTo);
		context.add("textFrom", textFrom);
		context.add("textTo", textTo);
		context.add("translatorService", translatorService);

		translateButton.addActionListener(new TranslatorController(context));

		this.add(translateButton);
		layout.putConstraint(SpringLayout.SOUTH, translateButton, -3,
				SpringLayout.SOUTH, this);
		layout.putConstraint(SpringLayout.EAST, translateButton, -3,
				SpringLayout.EAST, this);
	}
}
